﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Pr0ject0.Models
{
    public class ApplicantSessionManager
    {
        private enum ApplicantState {Jobs, Applications, Questionnaire, Finished };

        private volatile static ApplicantSessionManager singleton;
        private static readonly object lockObj = new object();

        private Dictionary<string, ApplicantState> states;
        private Dictionary<string, DateTime> sessions;
        private Dictionary<string, int[]> selectedJobIds;
        private Dictionary<string, List<JobApplicationModel>> joblist;

        public static ApplicantSessionManager GetInstance()
        {
             if (singleton == null)
            {
                lock (lockObj)
                {
                    if (singleton == null) singleton = new ApplicantSessionManager();
                }
            }

            return singleton;
        }

        public ApplicantSessionManager()
        {
            states =  new Dictionary<string, ApplicantState>() ;
            sessions = new Dictionary<string,DateTime>();
            selectedJobIds = new Dictionary<string,int[]>();
            joblist = new Dictionary<string,List<JobApplicationModel>>();
        }

        public string GetUniqueId()
        {
            Guid generator;
            Random rand = new Random();
            int i = rand.Next(3);

            generator = new Guid();

            string id = generator.ToString();

            sessions[id] = DateTime.Now.AddMinutes(5.0d);
            states[id] = ApplicantState.Jobs;

            return id;
        }

        public void AddJobIdArray(int[] ids, string id)
        {
            if (IsValidId(id))
            {
                if (states[id] == ApplicantState.Jobs)
                {
                    selectedJobIds[id] = ids;
                    sessions[id] = DateTime.Now.AddHours(5.0d);
                    states[id] = ApplicantState.Applications;
                }
            }
        }

        public bool IsValidId(string Id)
        {
            bool _ret = false;

            if(sessions.ContainsKey(Id))
            {
                _ret = 0 >= DateTime.Compare(DateTime.Now, sessions[Id]);
            }

            return _ret;
        }

        public void AddApplications(List<JobApplicationModel> apps, string id)
        {
            if (IsValidId(id))
            {
                if (states[id] == ApplicantState.Applications)
                {
                    joblist[id] = apps;
                    sessions[id] = DateTime.Now.AddMinutes(5.0d);
                    states[id] = ApplicantState.Questionnaire;
                }
            }
        }

        public void AddQuestionnaire(List<AnswerModel> answers, string id)
        {
            if (IsValidId(id))
            {
                if (states[id] == ApplicantState.Questionnaire)
                {
                    foreach (JobApplicationModel app in joblist[id])
                    {
                        app.answers = new List<AnswerModel>(answers);
                    }

                    JobApplicationDB.GetInstance().Add(joblist[id]);
                    states[id] = ApplicantState.Finished;
                }
            }
        }

        public int[] GetJobList(string id)
        {
            return selectedJobIds[id];
        }
    }
}